package com.szz.cardecision.controller;

import com.szz.cardecision.entity.Consultation;
import com.szz.cardecision.entity.User;
import com.szz.cardecision.mapper.ConsultationMapper;
import com.szz.cardecision.mapper.UserMapper;
import com.szz.cardecision.service.LLMService;
import jakarta.validation.constraints.NotBlank;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/consult")
public class ConsultationController {
    private final ConsultationMapper consultationMapper;
    private final UserMapper userMapper;
    private final LLMService llmService;

    public ConsultationController(ConsultationMapper consultationMapper, UserMapper userMapper, LLMService llmService) {
        this.consultationMapper = consultationMapper;
        this.userMapper = userMapper;
        this.llmService = llmService;
    }

    @GetMapping("/new")
    public String newConsult(Model model) {
        model.addAttribute("form", new ConsultForm());
        return "consultation_form";
    }

    @PostMapping("/new")
    public String createConsult(@ModelAttribute("form") ConsultForm form, Authentication auth) {
        User user = userMapper.findByUsername(auth.getName()).orElseThrow();
        Consultation c = new Consultation();
        c.setUserId(user.getId());
        c.setBudgetMin(form.getBudgetMin());
        c.setBudgetMax(form.getBudgetMax());
        c.setBodyType(form.getBodyType());
        c.setScenes(form.getScenes());
        c.setFuelType(form.getFuelType());
        c.setBrandPreference(form.getBrandPreference());

        // Generate advice via multiple LLMs
        String advice = llmService.generateCombinedAdvice(user, c);
        c.setResultText(advice);
        consultationMapper.insert(c);

        // Award points
        user.setPoints((user.getPoints() == null ? 0 : user.getPoints()) + 5);
        userMapper.update(user);

        return "redirect:/consult/" + c.getId();
    }

    @GetMapping("/history")
    public String history(Authentication auth, Model model) {
        User user = userMapper.findByUsername(auth.getName()).orElseThrow();
        List<Consultation> list = consultationMapper.findByUserIdOrderByCreatedAtDesc(user.getId());
        model.addAttribute("items", list);
        return "consultation_list";
    }

    @GetMapping("/{id}")
    public String detail(@PathVariable Long id, Authentication auth, Model model) {
        Consultation c = consultationMapper.findById(id).orElseThrow();
        model.addAttribute("c", c);
        boolean mine = userMapper.findById(c.getUserId()).map(u -> u.getUsername().equals(auth.getName())).orElse(false);
        model.addAttribute("mine", mine);
        return "consultation_detail";
    }

    public static class ConsultForm {
        private Integer budgetMin;
        private Integer budgetMax;
        @NotBlank
        private String bodyType;
        @NotBlank
        private String scenes;
        @NotBlank
        private String fuelType;
        private String brandPreference;

        public Integer getBudgetMin() { return budgetMin; }
        public void setBudgetMin(Integer budgetMin) { this.budgetMin = budgetMin; }
        public Integer getBudgetMax() { return budgetMax; }
        public void setBudgetMax(Integer budgetMax) { this.budgetMax = budgetMax; }
        public String getBodyType() { return bodyType; }
        public void setBodyType(String bodyType) { this.bodyType = bodyType; }
        public String getScenes() { return scenes; }
        public void setScenes(String scenes) { this.scenes = scenes; }
        public String getFuelType() { return fuelType; }
        public void setFuelType(String fuelType) { this.fuelType = fuelType; }
        public String getBrandPreference() { return brandPreference; }
        public void setBrandPreference(String brandPreference) { this.brandPreference = brandPreference; }
    }
}